import importlib
import os
import threading

from DBUtil import database
# 建一个文件，用于放置输出过的数据，便于添加内容不再重复调用接口
with open("filter", "r") as f:
    filter=f.read()

objects1=[]
objects2=[]

impl_list1 = os.listdir("biz/mysql")
impl_list2 = os.listdir("biz/pandas")
for impl_name in impl_list1:
    if not impl_name.endswith('.py'):
        continue
    module_name = impl_name.split(".py")[0]
    if module_name in filter:
        continue
    module = importlib.import_module(f"biz.mysql.{module_name}")
    try:
        objects1.append(getattr(module,module_name))
    except AttributeError:
        # 自定义异常信息
        print(f"Failed to get attribute {module_name} from module {module}.")
        # 利用 raise 语句重新引发异常，将其抛出
        continue

for impl_name in impl_list2:
    if not impl_name.endswith('.py'):
        continue
    module_name = impl_name.split(".py")[0]
    if module_name in filter:
        continue
    module = importlib.import_module(f"biz.pandas.{module_name}")
    try:
        objects2.append(getattr(module,module_name))
    except AttributeError:
        # 自定义异常信息
        print(f"Failed to get attribute {module_name} from module {module}.")
        # 利用 raise 语句重新引发异常，将其抛出
        continue

conn = database().connection()
with open("filter", "a") as f:
    for obj in objects1:
        ob = obj()
        print('mysql开始{}.....'.format(type(ob).__name__))
        ob.write(conn)
        f.write(type(ob).__name__)
        f.write(',')

def load_module(list):
    pass
# 考虑到pandas 任务可能时间较长,不耽误整体时间，另起线程
def run_pandas():
    with open("filter", "a") as f:
        for obj in objects2:
            ob = obj()
            print('pandas开始{}.....'.format(type(ob).__name__))
            ob.write(conn)
            f.write(type(ob).__name__)
            f.write(',')
thread=threading.Thread(target=run_pandas)
thread.start()
thread.join()

conn.close()
print('一键导出数据结束')
